class Solution {
public:
int f(vector<int>&arr,int m)
{
    int t=0;
    int ans=0;
    for(int l=0,r=0;l<arr.size();l++)
    {
        while(r+1<arr.size()&&arr[r+1]-arr[l]<=m)
        {
            r++;
        }
        ans+=r-l;
    }
    return ans;
}


    int smallestDistancePair(vector<int>& nums, int k) {
        sort(nums.begin(),nums.end());
        int l=0,r=nums[nums.size()-1],ans=0;
        while(l<r)
        {
            long long mid=l+(r-l)/2;
            if(f(nums,mid)>=k)
            {
                ans=mid;
                r=mid;
            }
            else 
            l=mid+1;
        }
            return ans;
    }
};;
